package com.yanqu.road.server.cmd.zhaoshang;

import com.yanqu.road.entity.messageSubscribe.entity.MessageUserNoticeData;
import com.yanqu.road.entity.zhaoshang.ZsWarReport;
import com.yanqu.road.entity.zhaoshang.enums.eZsReportType;
import com.yanqu.road.logic.pb.ZhaoShangPb;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.zhaoshang.ZsModule;
import com.yanqu.road.server.manger.messagesubscribe.MessageSubscribeMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.zhaoshang.ZsMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_ZS_WAR_REPORT_ADD_FROM_CROSS, desc = "战报新增")
public class ZsWarReportAddCmdFromCross implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {

        ZhaoShangProto.ZsWarReportAddFromCross msg = ZhaoShangProto.ZsWarReportAddFromCross.parseFrom(packet.getBytes());

        ZsWarReport zsWarReport = ZhaoShangPb.parseToZsWarReport(msg.getWarReportInfo());

        long playerId = packet.getPlayerId();

        GamePlayer player = GamePlayerMgr.getOnlinePlayer(playerId);
        if(player == null){//不在线
            //保存战报
            ZsMgr.addWarReport(zsWarReport);
            if(zsWarReport.getType() != eZsReportType.Complete.getIntValue()) {
                ZsMgr.addLogZsProjectSeatExpel(zsWarReport);
            }
        }else{ //在线

            //在线判重，与登录时同步可能重复
            List<ZsWarReport> list = new ArrayList<>();
            list.add(zsWarReport);
            player.getModule(ZsModule.class).syncWarReportFromCross(list, false);

            //推送红点
            player.getModule(ZsModule.class).syncRedDot();
        }

        if(zsWarReport.getType() == eZsReportType.Fail.getIntValue()){
            long userId = zsWarReport.getUserId();
            MessageUserNoticeData noticeData = MessageSubscribeMgr.getUserNoticeData(userId);
            noticeData.setBussinessPosRobTime(System.currentTimeMillis());
        }

    }
}
